﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;
using WebLayer;
using System.Net;
using HtmlAgilityPack;

namespace WebPageParser
{
    class ResultsTableEntry
    {
        public String Course = null;
        public String Unit = null;
        public String Grammar = null;
        public String Vocabulary = null;
        public String Listening = null;
        public String Reading = null;
        public String Avarage = null;
    }

    class ResultsWebPage: SiteWebPage
    {
        private ResultsTableEntry m_tableHeader = null;
        public  ResultsTableEntry TableHeader
        {
            get
            {
                if (m_tableHeader == null)
                {
                    Request();
                }
                return m_tableHeader;
            }
        }
        
        private List<ResultsTableEntry> m_tableContent = null;
        public  List<ResultsTableEntry> TableContent
        {
            get
            {
                if (m_tableContent == null)
                {
                    Request();
                }
                return m_tableContent;
            }
        }

        public ResultsWebPage():
        base("ResultsPage",GetActionPageUri("results"))
        {}

        protected override void OnRequest(HttpWebRequest request)
        {
            m_tableHeader = new ResultsTableEntry();
            m_tableContent = new List<ResultsTableEntry>();

            base.OnRequest(request);
        }

        protected override void OnResponse(HttpWebRequest request, HttpWebResponse response)
        {
            base.OnResponse(request, response);

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(PageContent);

            //Results html page has only one table. Select it;
            HtmlNode node = doc.DocumentNode.SelectSingleNode("//table");

            /* 5 collumns in a table */
            HtmlTableParser tableParser = new HtmlTableParser(7, node);
            tableParser.TransformTable<ResultsTableEntry>(TableContent, Transformer);
            m_tableHeader = Transformer(tableParser.Header, tableParser.HtmlHeader);
        }

        private ResultsTableEntry Transformer(HtmlTableRow textRow, HtmlTableRow htmlRow)
        {
            ResultsTableEntry entry = new ResultsTableEntry();
            entry.Course = textRow[0];
            entry.Unit = textRow[1];
            entry.Grammar = textRow[2];
            entry.Vocabulary = textRow[3];
            entry.Listening = textRow[4];
            entry.Reading = textRow[5];
            entry.Avarage = textRow[6];

            return entry;
        }

        
    }
}
